package org.hy.controller.group;

import static org.hy.util.AppConstant.SESSION_KEY_USER;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import org.hy.domain.User;
import org.hy.domain.group.GAnswer;
import org.hy.domain.group.GDiscuss;
import org.hy.domain.group.GMember;
import org.hy.service.group.IGDiscussService;
import org.hy.service.group.IGMemberService;
import org.hy.util.AppConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 * 讨论控制器
 * 目前各个处理请求的函数返回值为void,理应返回String的逻辑视图名称.
 * 在编写完页面后更改返回值为String
 * @author xwm
 *
 */
@Controller
@RequestMapping("/discuss/{gid}/")
@SessionAttributes(SESSION_KEY_USER)   //用于从session中取数据
public class GDiscussController {

	//注入的服务类
	@Autowired private IGDiscussService discussService;
	@Autowired private IGMemberService gMemberService;
	
	/**
	 *  显示小组讨论
	 * @param gid 小组id
	 * @param page 页号
	 * @param model 模型对象,用于给视图使用
	 */
	@RequestMapping("list/{page}")
	public void list(@PathVariable int gid,@PathVariable int page,ModelMap model){
		List<GDiscuss> discussList = discussService.findGDiscussWithGAnswerByGid(gid, page, AppConstant.PAGE_SIZE);
		model.addAttribute("discussList", discussList);
	}
	
	/**
	 * 显示第一页小组讨论
	 * @param gid 小组编号
	 */
	@RequestMapping("list")
	public void list(@PathVariable int gid) {
		 list(gid,1,new ModelMap());
	}
	
	/**
	 *  显示指定讨论的回复列表
	 * @param discussId 指定的讨论Id
	 * @param page 页号
	 * @param model 模型对象
	 */
	@RequestMapping("/listAnswer/{discussId}/{page}")
	public void listAnswers(@PathVariable int discussId,@PathVariable int page,ModelMap model) {
		GDiscuss discuss = discussService.findGDiscussWithGAnswerById(discussId, page, AppConstant.PAGE_SIZE);
		model.addAttribute("answers", discuss);
	}
	
	/**
	 * 显示第一页的 指定讨论的回复列表
	 * @param discussId 指定的讨论Id
	 */
	@RequestMapping("/listAnswer/{discussId}/}")
	public void listAnswers(@PathVariable int discussId) {
		listAnswers(discussId, 1, new ModelMap());
	}
	
	/**
	 * 用于跳转到新增讨论页面
	 */
	@RequestMapping(value={"new"})
	public void add() {
		
	}
	
	/**
	 * 用于跳转到添加评论的页面
	 */
	@RequestMapping("newAnswer")
	public void addAnswer() {
		
	}
	
	/**
	 * 添加讨论.
	 * @param gid 小组Id
	 * @param content 讨论内容
	 * @param loginedUser 登录用于,来自session
	 */
	@RequestMapping(value={"create"},method={POST})
	public void create(@PathVariable int gid,String content,@ModelAttribute(SESSION_KEY_USER) User loginedUser) {
		GDiscuss discuss = new GDiscuss();
		discuss.setContent(content);
		int uid = loginedUser.getId();
		GMember gm = gMemberService.findGMemberByUidAndGid(uid, gid);
		discuss.setPoster(gm);
		discuss.setIntime(AppConstant.now());
		
		discussService.addGDiscuss(discuss);
	}
	
	/**
	 * 添加评论
	 * @param pid 上级评论id
	 * @param discussId 讨论id
	 * @param content 回复内容
	 * @param photo 回复图片
	 * @param gid 小组id
	 * @param loginedUser 登录用户,
	 */
	@RequestMapping(value={"createAnswer"},method={POST})
	public void creatAnswer(int pid,int discussId,String content,String photo,@PathVariable int gid,@ModelAttribute(SESSION_KEY_USER) User loginedUser){
		GMember member = gMemberService.findGMemberByUidAndGid(loginedUser.getId(), gid);
		GAnswer answer = new GAnswer();
		answer.setContent(content);
		answer.getContent().setPhoto(photo);
		answer.setPid(pid);
		answer.setRefFeedId(discussId);
		answer.setPoster(member);
		
		discussService.addAnswer(answer);
	}
	/**
	 * 跳转到修改讨论页面
	 * @param gid 讨论id
	 */
	@RequestMapping("edit/{discussId}")
	public void edit(@PathVariable int discussId,ModelMap model) {
		GDiscuss discuss = discussService.findGDiscussById(discussId);
		model.addAttribute("discuss", discuss);
	}
	/**
	 * 跳转到修改评论页面
	 * @param answerId 评论id
	 * @param model 模型对象
	 */
	@RequestMapping("editAnswer/{answerId}")
	public void editAnswer(@PathVariable int answerId,ModelMap model) {
		GAnswer answer = discussService.findAnswerById(answerId);
		model.addAttribute("answer", answer);
	}
	
	/**
	 * 修改讨论
	 * @param discussId 讨论id
	 * @param content 内容
	 * @param photo 图片
	 */
	@RequestMapping(value={"update"},method={POST})
	public void update(int discussId,String content,String photo) {
		GDiscuss discuss = discussService.findGDiscussById(discussId);
		discuss.setContent(content);
		discuss.getContent().setPhoto(photo);
		
		discussService.updateGDiscuss(discuss);
	}
	/**
	 * 修改评论
	 * @param answerId 评论id
	 * @param content 内容
	 * @param photo 图片
	 */
	@RequestMapping(value={"updateAnswer"},method={POST})
	public void updateAnswer(int answerId,String content,String photo){
		GAnswer answer = discussService.findAnswerById(answerId);
		answer.setContent(content);
		answer.getContent().setPhoto(photo);
		
		discussService.updateGAnswer(answer);
	}
	
	/**
	 * 删除讨论
	 * @param discussId 讨论id
	 * @param loginedUser 登录用户
	 * @param gid 小组id
	 */
	@RequestMapping("{delete/{discussId}")
	public void delete(@PathVariable int discussId,User loginedUser,@PathVariable int gid){
		GMember member = gMemberService.findGMemberByUidAndGid(loginedUser.getId(), gid);
		discussService.deleteGDiscuss(discussId, member.getId());
	}
	
	/**
	 * 删除评论
	 * @param answerId 评论id
	 * @param loginedUser 登录用户
	 * @param gid 小组id
	 */
	@RequestMapping("{deleteAnswer/{answerId}")
	public void deleteAnswer(@PathVariable int answerId,@ModelAttribute(SESSION_KEY_USER) User loginedUser,@PathVariable int gid) {
		GMember member = gMemberService.findGMemberByUidAndGid(loginedUser.getId(), gid);
		discussService.deleteGAnswer(answerId, member.getId());
	}
}
